package com.service.impl;

import com.dao.FriendDao;
import com.dao.FriendMessageDao;
import com.dao.FriendTypeDao;
import com.entity.Friend;
import com.entity.FriendMessage;
import com.entity.FriendType;
import com.service.FriendTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class FriendTypeServiceImpl implements FriendTypeService {
    @Autowired
    private FriendTypeDao friendTypeDao;
    @Autowired
    private FriendMessageDao friendMessageDao;

    @Override
    @Transactional
    public List<FriendType> getByUserId(Integer id) {
        List<FriendType> friendTypes = friendTypeDao.getByUserId(id);
        for (FriendType friendType : friendTypes) {
            List<Friend> friends = friendType.getFriends();
            for (Friend friend : friends) {
                //查询和某个好友最新的聊天消息
                FriendMessage newFriendMessage = friendMessageDao.getNewFriendMessage(friend.getUserId(),friend.getFriendId());
                //设置子最新聊天消息
                friend.setNewFriendMessage(newFriendMessage);
                //查询和好友有多少条未读消息
                Integer notReadCount = friendMessageDao.getNotReadCount(friend.getUserId(),friend.getFriendId());
                friend.setNotReadCount(notReadCount);
            }
        }
        return friendTypes;
    }

    //添加
    @Override
    public Boolean addFriendType(String typeName, Integer userId) {
        return friendTypeDao.addFriendType(typeName, userId);
    }

    //修改分组名
    @Override
    public Boolean editFriendType(String typeName, Integer id) {
        return friendTypeDao.editFriendType(typeName, id);
    }

    //删除
    @Override
    public Boolean delFriendType(Integer id) {
        return friendTypeDao.delFriendType(id);
    }
}
